#!/usr/bin/env bash

#get this script real directory
SOURCE="$0"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    # if $SOURCE was a relative symlink, 
    #we need to resolve it relative to the path 
    #where the symlink file was located
    [[ $SOURCE != /*  ]] && SOURCE="$this_script_dir/$SOURCE"
done
this_script_dir="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

log_level=${log_level:=info}

assist_dir=${assist_dir:=~/git/tool_sh/assist}
source $assist_dir/global.sh
################################################################################
#                                                                          80col
print_help() 
{
    cat <<EOF
    useage: 
    $0 --username=<root> ...

    notice: parameter's number must >= 5

    --exist              - check image name:tag whether exist, shoud come with --image 
    --create=monitor     - create image, set name and tag by --image,default empty to create base image
    --run                - create a container, shoud set --host_name & --image & --dockerfile

    --host_name=<host>   - container's name, needed by --run
    --image=<name:tag>   - docker image name and tag
    --log_level=<debug>  - control pint log on streen,trace|debug|info|warn|error|critical
    --dockerfile         - d1|ubuntu_18.04(default)|ubuntu_20.04

    --help  - prints help screen, script will exit after printing


EOF
}


#return 0: exist
#return 1: not exist
is_exist_image()
{
    local param=$1
    local name=${param%:*}
    local tag=${param#*:}

    local ret=`docker images | awk '$1=="'$name'" && $2=="'$tag'"{printf "%s\n",$3}'`
    if [ -z "$ret" ] ; then
        #not exist
        return 1;
    else
        #exist
        return 0;
    fi
}

log_out_image_info()
{
    local param=$1
    local name=${param%:*}
    local tag=${param#*:}
    docker images | awk '$1=="'$name'" && $2=="'$tag'"{printf "%s\n",$0}'
}

get_image_id()
{
    local param=$1
    local name=${param%:*}
    local tag=${param#*:}
    local ret=`docker images | awk '$1=="'$name'" && $2=="'$tag'"{printf "%s",$3}'`
    printf "$ret"
}

create_base_image()
{
    local base_image=ubuntu:18.04.v1
    local d1_dir=$this_script_dir/ubuntu_18.04
    is_exist_image $base_image
    if [ $? -ne 0 ] ; then #not exist
        docker build -f $d1_dir/dockerfile -t $base_image $d1_dir
    fi 

}

main()
{
    if [ -n "$help" ] || [ $_arg_num_ -eq 0 ]; then
        print_help
        exit 0
    elif [ -n "$exist" ] ; then
        check_argument exist image || exit 1

        is_exist_image $image
        if [ $? -eq 0 ] ; then
            image_id=`get_image_id $image`

            vlogi "image exist, image name:tag is: $image , id:$image_id , detial is:"
            log_out_image_info $image
            exit 0

        else
            vlogi "image not exist"
            exit 1
        fi

    elif [ "$create" == "monitor" ] ; then #base on base_image(ubuntu:18.04.v1) to crate monitor's image
        check_argument create image  || exit 1

        d1_dir=$this_script_dir/ubuntu_18.04
        create_base_image

        is_exist_image $image 
        if [ $? -eq 0 ] ; then #exist
            vlogi "image $image already exist"
        else
            d1_dir=$this_script_dir/ubuntu_18.04
            vlogi "start to execute : \"docker build -f $d1_dir/dockerfile.monitor -t $image $d1_dir\""
            docker build -f $d1_dir/dockerfile.monitor -t $image $d1_dir
            vlogi "image $image build succeed."
        fi

    elif [ -n "$create" ] ; then
        check_argument create image dockerfile || exit 1

        is_exist_image $image 
        if [ $? -eq 0 ] ; then #exist
            vlogi "image already exist"
        else
            if [ -n "$dockerfile" ] ; then
                d1_dir=$this_script_dir/$dockerfile
            else
                d1_dir=$this_script_dir/$ubuntu_18.04  #default
            fi

            vlogi "start to execute : \"docker build -f $d1_dir/dockerfile -t $image $d1_dir\""
            docker build -f $d1_dir/dockerfile -t $image $d1_dir
            vlogi "image build succeed."
        fi
    elif [ -n "$run" ] ; then
        check_argument run host_name image || exit 1
        #docker run -it -d -v /etc/localtime:/etc/localtime:ro -v ~/git1:/root/git --ulimit core=-1 --security-opt seccomp=unconfined --privileged=true --name=ua1 -h ua1 ua:v1  bash
        if [ "$run" != "run" ] ; then
            param_run="$run"
        fi
        docker run -it -d $param_run -v /etc/localtime:/etc/localtime:ro -v ~/git1:/root/git --ulimit core=-1 --security-opt seccomp=unconfined --privileged=true --name=$host_name -h $host_name $image  bash

    fi
}

main
exit 0








